<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
class UserdetailsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addUserdetails($user_id,$userdetails)
    {
		if(isset($userdetails['districts'])){
			$district_id=$userdetails['districts'];
			$area_id=$userdetails['areas'];
			$location_id=$userdetails['location_id'];
		}else{
			$district_id='';
			$area_id='';
			$location_id='';
		}
		
		$data = array(
			'user_id' 	  		 => $user_id, 	
			'secondary_email'    => '', 	
			'display_name'       => '', 	
			'phone_number'       => $userdetails['phoneNumber'], 	
			'alter_phone_mobile' => '', 	
			'country_id' 	     => '', 
			'state_id' 	         => '',	
			'district_id' 		 => $district_id, 	
			'area_id' 			 => $area_id,   
			'location_id' 		 => $location_id,   
			'city_name' 		 => '',   
			'address' 			 => $userdetails['address'],   
			'pincode' 			 => '',   
			'status' 		     => 1, 
			'added_date' 	     => date('Y-m-d H:i:s'), 				
			'updated_date' 	     => date('Y-m-d H:i:s'), 				
		);		
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;		
    }
    public function editUser($id)
    {		
		$select = $this->tableGateway->getSql()->select()			
				->where('userId= "'.$id.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);	
		$row = $resultSet->current();		
		if (!$row) {
            throw new \Exception("Could not find row $id");
        }		
        return $row;
	}	
	public function updateUserDetails($userdetails){
		if(isset($userdetails['districts'])){
			$district_id=$userdetails['districts'];
			$area_id=$userdetails['areas'];
			$location_id=$userdetails['location_id'];
		}else{
			$district_id='';
			$area_id='';
			$location_id='';
		}
		$data = array(
			'user_id'            =>$userdetails['hid_user_id'],
			'secondary_email'    =>$userdetails['secondEmail'], 	
			'display_name'       =>$userdetails['displayName'], 	
			'phone_number'       =>$userdetails['phoneNumber'], 	
			'alter_phone_mobile' =>$userdetails['alternateNumber'], 	
			'country_id' 	     =>'', 
			'state_id' 	         =>'',	
			'district_id' 		 =>$district_id, 	
			'area_id' 			 =>$area_id,   
			'location_id' 		 =>$location_id,   
			'city_name' 		 =>$userdetails['cityname'],   
			'address' 			 =>$userdetails['address'],   
			'pincode' 			 =>$userdetails['pincode'],  				
		);
		$update_userdetails_id=$this->tableGateway->update($data, array('user_details_id' => $userdetails['hid_user_details_id']));			
		return $update_userdetails_id;	
	}
	public function addguestdetails($user_id,$phone)
    {			
		$data = array(
			'userId' 	  		=> $user_id, 	
			'mobile_number' 	=> $phone,			
			'user_status' 		=> '1', 
			'last_updateddate' 	=> date('Y-m-d H:i:s'), 
		);	
		$result=$this->tableGateway->insert($data);		
		return $result;		
    }
	public function checkUserId()
    {	
	 $select = $this->tableGateway->getSql()->select()			
			->where('userId="'.$_SESSION['user']['user_id'].'"');					 
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();
		return $row;		
	}
	public function addImage($img,$val){
		$data = array(
			'user_photo' 	=>	$img,
			'userId' 		=>	$_SESSION['user']['user_id'],
		);
		if($val=='insert'){
			$this->tableGateway->insert($data);
			return $this->tableGateway->lastInsertValue;
		}else{
			$updateImg=$this->tableGateway->update($data, array('userId' => $_SESSION['user']['user_id']));			
			return 	$updateImg;	
		}
	}
	public function deleteUsersDetails($ids){		
		$this->tableGateway->delete(array('(userId IN ('.$ids.'))'));			
		return $this->tableGateway->lastInsertValue;	
	}
}